package com.earogya.logic.patient_details;

import java.util.ArrayList;
import java.util.List;
import org.apache.struts.ehr.patient_details.model.*;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.group.ADR_A19_QUERY_RESPONSE;
import ca.uhn.hl7v2.model.v251.message.ADR_A19;
import ca.uhn.hl7v2.model.v251.segment.PID;

/**
 * 
 * Manage ADR A19 message which contains the results for a query message
 * 
 * @author Ureka
 * 
 */

public class ADR_A19_Handler {
	private ADR_A19 adtMsg;
	private List<PatientDetails> patientDetailsList = new ArrayList<PatientDetails>();

	public ADR_A19_Handler() {

	}

	public ADR_A19_Handler(ADR_A19 adtMsg) {
		this.adtMsg = adtMsg;
	}

	
	/**
	 * 
	 * Identify the segments and fields of the message and assign them to objects
	 * 
	 * @return
	 * @throws DataTypeException
	 * @throws HL7Exception
	 */
	public List<PatientDetails> manageMessage() throws DataTypeException,
			HL7Exception {
		for (int i = 0; i < adtMsg.getQUERY_RESPONSEReps(); i++) {
			patientDetailsList.add(new PatientDetails());
			ADR_A19_QUERY_RESPONSE response = adtMsg.getQUERY_RESPONSE(i);
			Patient patient = patientDetailsList.get(i).getPatient();
			PID pid = response.getPID();
			patient.setPatientId(pid.getPatientID().getIDNumber().getValue());
			patient.setPrefix(pid.getPatientName(0).getPrefixEgDR().getValue());
			patient.setGivenName(pid.getPatientName(0).getGivenName()
					.getValue());
			patient.setFamilyName(pid.getPatientName(0).getFamilyName()
					.getSurname().getValue());
			patient.setGender(pid.getAdministrativeSex().getValue());
			patient.setNicNo(pid.getPatientIdentifierList(0).getIDNumber()
					.getValue());
			if (pid.getDateTimeOfBirth().getTime().getValue() == null) {
			} else {
				patient.setBirthTime(pid.getDateTimeOfBirth().getTime()
						.getValueAsDate());
			}
			patient.setBirthPlaceAddress(pid.getBirthPlace().toString());
			patient.setMaritialStatus(pid.getMaritalStatus().getText()
					.toString());
			patient.setEthnicGroupId(pid.getEthnicGroup(0).getText().toString());
			patient.setReligiousAffliationId(pid.getReligion().getText()
					.toString());

		}
		this.enterPatientAddressDetails();
		this.enterPatientEmailAndTelephoneDetails();
		return patientDetailsList;
	}

	
	/**
	 * 
	 * Assign patient address details to the object
	 * 
	 * @throws HL7Exception
	 */
	private void enterPatientAddressDetails() throws HL7Exception {
		for (int i = 0; i < adtMsg.getQUERY_RESPONSEReps(); i++) {
			ADR_A19_QUERY_RESPONSE response = adtMsg.getQUERY_RESPONSE(i);
			PID pid = response.getPID();
			PatientAddress patientAddress = patientDetailsList.get(0)
					.getPatientAddressList();

			patientDetailsList
					.get(i)
					.getPatientAddressList()
					.setPatientId(
							this.patientDetailsList.get(i).getPatient()
									.getPatientId());
			patientAddress.setCity1(pid.getPatientAddress(0)
					.getOtherDesignation().getValue());
			patientAddress.setCountry(pid.getPatientAddress(0).getCountry()
					.getValue());
			patientAddress.setHouseNumber(pid.getPatientAddress(0)
					.getStreetAddress().getDwellingNumber().getValue());
			patientAddress.setCity2(pid.getPatientAddress(0).getCity()
					.getValue());
			patientAddress.setStreet(pid.getPatientAddress(0)
					.getStreetAddress().getStreetName().getValue());
			System.out.print(pid.getPatientAddress(0).getStreetAddress()
					.getDwellingNumber().getValue());

		}
	}

	
	/**
	 * 
	 * Assign patient email and telephone details to the objects
	 * 
	 */
	private void enterPatientEmailAndTelephoneDetails() {
		for (int j = 0; j < adtMsg.getQUERY_RESPONSEReps(); j++) {
			ADR_A19_QUERY_RESPONSE response = adtMsg.getQUERY_RESPONSE(j);
			PID pid = response.getPID();
			int i = 0;
			while (!(pid.getPhoneNumberHome(i).getTelephoneNumber().getValue() == null)
					|| !(pid.getPhoneNumberHome(i).getEmailAddress().getValue() == null)) {

				if (pid.getPhoneNumberHome(i).getTelephoneNumber().getValue() == null) {
				} else {
					patientDetailsList.get(j).getPatientTelephoneList().add(new PatientTelephone());
					patientDetailsList.get(j).getPatientTelephoneList().get(i).setPatientId(
									this.patientDetailsList.get(j).getPatient().getPatientId());
					patientDetailsList.get(j).getPatientTelephoneList().get(i).setTelephoneNo(
									pid.getPhoneNumberHome(i).getTelephoneNumber().getValue());
				}
				if (pid.getPhoneNumberHome(i).getEmailAddress().getValue() == null) {
				} else {
					patientDetailsList.get(j).getPatientEmailList().add(new PatientEmail());
					patientDetailsList.get(j).getPatientEmailList().get(i).setPatientId(
									this.patientDetailsList.get(j).getPatient().getPatientId());
					patientDetailsList.get(j).getPatientEmailList().get(i).setEmail(
									pid.getPhoneNumberHome(i).getEmailAddress().getValue());
				}
				i++;
			}
		}

	}

}
